package com.ebt.m.fileExplore.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Window;
import android.widget.FrameLayout;

import com.ebt.m.R;
import com.ebt.m.fileExplore.event.FilePickEvent;
import com.ebt.m.fileExplore.interfaces.IBackHandlerActivity;
import com.ebt.m.fileExplore.interfaces.IBackHandlerFragment;
import com.ebt.m.fileExplore.ui.FragmentFileExplore.FileExploreListener;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class ActivityFileExplore extends FragmentActivity implements IBackHandlerActivity{

	private static final String TAG = "ActivityFileExplore";
	public static final String RETURN_FILE = "file";
	public static final String RETURN_FILE_NAME = "file_name";
	public static final String RETURN_FILE_PATH = "file_path";
	public static final String RETURN_FILE_LENGTH = "file_length";
	private FrameLayout frame;
	private String mFileExploreMode;
	private String mFileExploreRootDir;
	private FragmentFileExplore mFragmentFileExplore;
	private FileExploreListener fileExploreListener;
	private Fragment backHandledFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWindow();
		this.setContentView(R.layout.activity_file_explore);

		frame = (FrameLayout)this.findViewById(R.id.frame);
	
		fileExploreListener = new FileExploreListener() {
			@Override
			public void result(File file) {
				Log.d(TAG, "file explore select file -> " + file);
				setResultForPickedFile(file);
			}
		};
		setDefaultFragment(mFileExploreMode, mFileExploreRootDir, R.id.frame, fileExploreListener);
	}

	public void initWindow(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	private void setResultForPickedFile(File file){
		Intent data = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString(RETURN_FILE_NAME, file.getName());
		bundle.putString(RETURN_FILE_PATH, file.getPath());
		bundle.putLong(RETURN_FILE_LENGTH, file.length());
		bundle.putSerializable(RETURN_FILE, file);
		data.putExtras(bundle);
		setResult(Activity.RESULT_OK, data);
		EventBus.getDefault().post(new FilePickEvent(file, FilePickEvent.FromFilePicker, 0));
		finish();
	}
	
	private void setDefaultFragment(String fileExploreMode, String fileExploreRootDir, int viewId, FileExploreListener fileExploreListener){  
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		mFragmentFileExplore = FragmentFileExplore.newInstance(fileExploreMode, fileExploreRootDir, fileExploreListener);
		transaction.replace(viewId, mFragmentFileExplore);
		transaction.commit();  
	}

	@Override
	public void setSelectedFragment(Fragment backHandledFragment) {
		this.backHandledFragment = backHandledFragment;
	}
	
	@Override
	public void onBackPressed() {
		if(handleFileExploreFragment()){
			return;
		}
		super.onBackPressed();
	}
	
	private boolean handleFileExploreFragment(){
		if(backHandledFragment != null && backHandledFragment instanceof IBackHandlerFragment){
			IBackHandlerFragment fragment = (IBackHandlerFragment)backHandledFragment;
			if(fragment instanceof FragmentFileExplore){
				Object[] result = fragment.onBackPressed();
				Boolean state = (Boolean)result[0];
				
				if(state != null && state == true){
					return true;
				}
			}
		}
		return false;
	}
}
